<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户反馈与工单管理系统 - 登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #007bff;
            --admin-color: #dc3545;
            --agent-color: #28a745;
            --customer-color: #17a2b8;
            --gradient-bg: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        body {
            background: var(--gradient-bg);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .login-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            overflow: hidden;
            max-width: 400px;
            width: 100%;
        }

        .login-header {
            background: var(--primary-color);
            color: white;
            padding: 30px;
            text-align: center;
            position: relative;
        }

        .login-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, transparent 100%);
        }

        .login-header h2 {
            margin: 0;
            font-weight: 600;
            position: relative;
            z-index: 1;
        }

        .login-header p {
            margin: 10px 0 0 0;
            opacity: 0.9;
            position: relative;
            z-index: 1;
        }

        .role-selector {
            padding: 20px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
        }

        .role-option {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            margin: 8px 0;
            border: 2px solid transparent;
            border-radius: 10px;
            cursor: pointer;
            transition: all 0.3s ease;
            background: white;
        }

        .role-option:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .role-option.active {
            border-color: var(--primary-color);
            background: rgba(0, 123, 255, 0.05);
        }

        .role-option.admin.active {
            border-color: var(--admin-color);
            background: rgba(220, 53, 69, 0.05);
        }

        .role-option.agent.active {
            border-color: var(--agent-color);
            background: rgba(40, 167, 69, 0.05);
        }

        .role-option.customer.active {
            border-color: var(--customer-color);
            background: rgba(23, 162, 184, 0.05);
        }

        .role-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
            font-size: 18px;
            color: white;
        }

        .role-icon.admin {
            background: var(--admin-color);
        }

        .role-icon.agent {
            background: var(--agent-color);
        }

        .role-icon.customer {
            background: var(--customer-color);
        }

        .role-info h6 {
            margin: 0;
            font-weight: 600;
            color: #333;
        }

        .role-info small {
            color: #666;
            font-size: 0.85em;
        }

        .login-form {
            padding: 30px;
        }

        .form-floating {
            margin-bottom: 20px;
        }

        .form-floating input {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            transition: all 0.3s ease;
        }

        .form-floating input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
        }

        .btn-login {
            width: 100%;
            padding: 12px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 16px;
            transition: all 0.3s ease;
            border: none;
            position: relative;
            overflow: hidden;
        }

        .btn-login::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .btn-login:hover::before {
            left: 100%;
        }

        .btn-login:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .loading-spinner {
            display: none;
            width: 20px;
            height: 20px;
            border: 2px solid #ffffff;
            border-top: 2px solid transparent;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .alert {
            border-radius: 10px;
            border: none;
            margin-bottom: 20px;
        }

        .demo-accounts {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-top: 20px;
        }

        .demo-accounts h6 {
            color: #495057;
            margin-bottom: 15px;
            font-weight: 600;
        }

        .demo-account {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 12px;
            margin: 5px 0;
            background: white;
            border-radius: 6px;
            border: 1px solid #e9ecef;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .demo-account:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }

        .demo-account .role-badge {
            font-size: 0.75em;
            padding: 2px 8px;
            border-radius: 12px;
            font-weight: 600;
        }

        .role-badge.admin {
            background: var(--admin-color);
            color: white;
        }

        .role-badge.agent {
            background: var(--agent-color);
            color: white;
        }

        .role-badge.customer {
            background: var(--customer-color);
            color: white;
        }

        .footer {
            text-align: center;
            padding: 20px;
            color: #6c757d;
            font-size: 0.9em;
        }

        .footer a {
            color: var(--primary-color);
            text-decoration: none;
        }

        .footer a:hover {
            text-decoration: underline;
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 10px;
            }
            
            .login-card {
                border-radius: 15px;
            }
            
            .login-header {
                padding: 20px;
            }
            
            .login-form {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <!-- 登录头部 -->
            <div class="login-header">
                <h2><i class="fas fa-ticket-alt me-2"></i>工单管理系统</h2>
                <p>客户反馈与工单管理平台</p>
            </div>

            <!-- 角色选择器 -->
            <div class="role-selector">
                <h6 class="mb-3">选择登录角色</h6>
                <div class="role-option admin" data-role="ADMIN">
                    <div class="role-icon admin">
                        <i class="fas fa-crown"></i>
                    </div>
                    <div class="role-info">
                        <h6>管理员</h6>
                        <small>系统管理、用户管理、全部权限</small>
                    </div>
                </div>
                <div class="role-option agent" data-role="AGENT">
                    <div class="role-icon agent">
                        <i class="fas fa-headset"></i>
                    </div>
                    <div class="role-info">
                        <h6>客服人员</h6>
                        <small>工单处理、客户服务、反馈管理</small>
                    </div>
                </div>
                <div class="role-option customer active" data-role="CUSTOMER">
                    <div class="role-icon customer">
                        <i class="fas fa-user"></i>
                    </div>
                    <div class="role-info">
                        <h6>客户</h6>
                        <small>提交工单、查看反馈、系统使用</small>
                    </div>
                </div>
            </div>

            <!-- 登录表单 -->
            <div class="login-form">
                <form id="loginForm">
                    <div class="alert alert-danger" id="errorAlert" style="display: none;">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        <span id="errorMessage"></span>
                    </div>

                    <div class="form-floating">
                        <input type="text" class="form-control" id="username" name="username" placeholder="用户名" required>
                        <label for="username"><i class="fas fa-user me-2"></i>用户名</label>
                    </div>

                    <div class="form-floating">
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码" required>
                        <label for="password"><i class="fas fa-lock me-2"></i>密码</label>
                    </div>

                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" id="rememberMe">
                        <label class="form-check-label" for="rememberMe">
                            记住我
                        </label>
                    </div>

                    <button type="submit" class="btn btn-primary btn-login" id="loginBtn">
                        <div class="loading-spinner" id="loadingSpinner"></div>
                        <span id="loginText">登录</span>
                    </button>
                </form>

                <!-- 注册链接 -->
                <div class="text-center mb-3">
                    <p>还没有账号？<a href="register.html" class="text-decoration-none">立即注册</a></p>
                </div>

                <!-- 演示账号 -->
                <div class="demo-accounts">
                    <h6><i class="fas fa-info-circle me-2"></i>演示账号</h6>
                    <div class="demo-account" data-username="admin" data-password="admin123">
                        <div>
                            <strong>admin</strong> / admin123
                        </div>
                        <span class="role-badge admin">管理员</span>
                    </div>
                    <div class="demo-account" data-username="agent1" data-password="agent123">
                        <div>
                            <strong>agent1</strong> / agent123
                        </div>
                        <span class="role-badge agent">客服</span>
                    </div>
                    <div class="demo-account" data-username="testuser" data-password="Test123">
                        <div>
                            <strong>testuser</strong> / Test123
                        </div>
                        <span class="role-badge customer">客户</span>
                    </div>
                </div>
            </div>

            <!-- 页脚 -->
            <div class="footer">
                <p>&copy; 2025 客户反馈与工单管理系统</p>
                <p>
                    <a href="#" onclick="showHelp()">帮助中心</a> | 
                    <a href="#" onclick="showContact()">联系我们</a>
                </p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 全局变量
        let selectedRole = 'CUSTOMER';
        const API_BASE_URL = 'http://localhost:9090/api';

        // DOM 元素
        const roleOptions = document.querySelectorAll('.role-option');
        const loginForm = document.getElementById('loginForm');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const loginBtn = document.getElementById('loginBtn');
        const loadingSpinner = document.getElementById('loadingSpinner');
        const loginText = document.getElementById('loginText');
        const errorAlert = document.getElementById('errorAlert');
        const errorMessage = document.getElementById('errorMessage');
        const demoAccounts = document.querySelectorAll('.demo-account');

        // 角色选择
        roleOptions.forEach(option => {
            option.addEventListener('click', () => {
                // 移除所有活动状态
                roleOptions.forEach(opt => opt.classList.remove('active'));
                // 添加当前活动状态
                option.classList.add('active');
                selectedRole = option.dataset.role;
                
                // 更新按钮颜色
                updateLoginButtonColor();
            });
        });

        // 更新登录按钮颜色
        function updateLoginButtonColor() {
            loginBtn.className = 'btn btn-login';
            switch(selectedRole) {
                case 'ADMIN':
                    loginBtn.classList.add('btn-danger');
                    break;
                case 'AGENT':
                    loginBtn.classList.add('btn-success');
                    break;
                case 'CUSTOMER':
                    loginBtn.classList.add('btn-info');
                    break;
                default:
                    loginBtn.classList.add('btn-primary');
            }
        }

        // 演示账号点击
        demoAccounts.forEach(account => {
            account.addEventListener('click', () => {
                const username = account.dataset.username;
                const password = account.dataset.password;
                
                usernameInput.value = username;
                passwordInput.value = password;
                
                // 根据用户名确定角色
                if (username === 'admin') {
                    selectRole('ADMIN');
                } else if (username.startsWith('agent')) {
                    selectRole('AGENT');
                } else if (username.startsWith('testuser')) {
                    selectRole('CUSTOMER');
                }
            });
        });

        // 选择角色
        function selectRole(role) {
            roleOptions.forEach(opt => {
                opt.classList.remove('active');
                if (opt.dataset.role === role) {
                    opt.classList.add('active');
                }
            });
            selectedRole = role;
            updateLoginButtonColor();
        }

        // 显示错误信息
        function showError(message) {
            errorMessage.textContent = message;
            errorAlert.style.display = 'block';
            setTimeout(() => {
                errorAlert.style.display = 'none';
            }, 5000);
        }

        // 显示加载状态
        function showLoading() {
            loginBtn.disabled = true;
            loadingSpinner.style.display = 'inline-block';
            loginText.textContent = '登录中...';
        }

        // 隐藏加载状态
        function hideLoading() {
            loginBtn.disabled = false;
            loadingSpinner.style.display = 'none';
            loginText.textContent = '登录';
        }

        // 登录请求
        async function login(username, password) {
            try {
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                const data = await response.json();

                if (data.success) {
                    // 存储用户信息
                    localStorage.setItem('token', data.data.token);
                    localStorage.setItem('user', JSON.stringify(data.data));
                    localStorage.setItem('role', data.data.role);
                    
                    console.log('登录成功，Token已保存:', data.data.token);
                    
                    // 根据角色跳转
                    redirectToDashboard(data.data.role);
                } else {
                    showError(data.message || '登录失败');
                }
            } catch (error) {
                console.error('登录错误:', error);
                showError('网络错误，请检查连接');
            }
        }

        // 根据角色跳转到对应仪表板
        function redirectToDashboard(role) {
            switch(role) {
                case 'admin':
                    window.location.href = 'admin-dashboard.html';
                    break;
                case 'agent':
                    window.location.href = 'agent-dashboard.html';
                    break;
                case 'customer':
                    window.location.href = 'customer-dashboard.html';
                    break;
                default:
                    window.location.href = 'dashboard.html';
            }
        }

        // 表单提交
        loginForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const username = usernameInput.value.trim();
            const password = passwordInput.value.trim();

            if (!username || !password) {
                showError('请输入用户名和密码');
                return;
            }

            showLoading();
            await login(username, password);
            hideLoading();
        });

        // 帮助和联系功能
        function showHelp() {
            alert('帮助中心功能正在开发中...');
        }

        function showContact() {
            alert('联系方式：\n邮箱：support@example.com\n电话：400-123-4567');
        }

        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', () => {
            // 检查是否已经登录
            const token = localStorage.getItem('token');
            if (token) {
                // 验证token是否有效
                validateToken();
            }
            
            // 初始化按钮颜色
            updateLoginButtonColor();
        });

        // 验证token
        async function validateToken() {
            try {
                const response = await fetch(`${API_BASE_URL}/auth/me`, {
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('token')}`
                    }
                });

                if (response.ok) {
                    const data = await response.json();
                    if (data.success) {
                        // Token有效，直接跳转
                        redirectToDashboard(data.data.role);
                    }
                }
            } catch (error) {
                // Token无效，清除本地存储
                localStorage.clear();
            }
        }
    </script>
</body>
</html>